﻿#include "user_page_config.h"

lv_obj_t* bat_value_label[4];  //电池数值显示
lv_obj_t* bat_img[4];
lv_obj_t* sign_img[4];


lv_obj_t* fast_img;
lv_obj_t* fast_value_label;
lv_obj_t* local_bat_img;     //本地电池数值显示
lv_obj_t* local_time_label;  //时间数值显示

lv_obj_t* deep_label;  //深度数值显示
lv_obj_t* temp_label;  //温度数值显示


lv_obj_t* compass; //方位数值显示对象

static lv_timer_t* timer;

static const lv_point_t line_points1[] = { {99,24},{99,24+36} };
static const lv_point_t line_points2[] = { {120,183},{120,183 + 12} };


const lv_point_t bat_pos[] =
{
    {20,102},
    {179,102},
    {20, 144},
    {179, 144},
};


const lv_point_t sign_pos[] = { {45,110},{195,110} };



//绑定主界面的速度值 图片资源 
const void* fast_value_image[] = {
[0] = (void*)&speed0,
[1] = (void*)&speed1,
[2] = (void*)&speed2,
[3] = (void*)&speed3,
[4] = (void*)&speed4,
[5] = (void*)&speed5,
[6] = (void*)&speed6,
[7] = (void*)&speed7,
[8] = (void*)&speed8,
[9] = (void*)&speed9,
[10] = (void*)&speed10,
};



//绑定主界面的电量值 图片资源 
const void* batt_value_image[] = {
[0] = (void*)&battery_blank,
[1] = (void*)&battery4,
[2] = (void*)&battery0,
[3] = (void*)&battery1,
[4] = (void*)&battery2,
[5] = (void*)&battery3,
};


//绑定主界面的电量值 图片资源 
const void* sign_value_image[] = {
[0] = (void*)&blank_signal,
[1] = (void*)&signal0,
[2] = (void*)&signal1,
[3] = (void*)&signal2,
[4] = (void*)&signal3,
};













// void user_main_page_entry(lv_obj_t* obj);
// void user_main_page_exit(lv_obj_t* obj);


static void main_event_handler(lv_event_t* e)
{
	//printf("%s\r\n",__FUNCTION__);
    lv_event_code_t event = lv_event_get_code(e);
    //lv_obj_t *obj = lv_event_get_target(e);
    lv_obj_t* obj = e->user_data;
    if (!lv_obj_is_valid(obj))
        return;
    if (event == LV_EVENT_DELETE)
    {
        printf("LV_EVENT_DELETE\r\n");
    }
    else if (event == LV_EVENT_CLICKED)
    {
        printf("LV_EVENT_CLICKED event=%d\r\n", event);

       
    }

    else if (event == LV_EVENT_FOCUSED)
    {
        //lv_obj_add_state(obj, LV_STATE_FOCUS_KEY | LV_STATE_FOCUSED | LV_STATE_PRESSED);
        //printf("LV_EVENT_FOCUSED event=%d\r\n", event);
    }
    else if (event == LV_EVENT_DEFOCUSED)
    {
        //printf("LV_EVENT_DEFOCUSED event=%d\r\n", event);
        //lv_obj_clear_state(obj, LV_STATE_FOCUS_KEY | LV_STATE_FOCUSED | LV_STATE_PRESSED);    /*Remove the pressed state manually*/

    }
    else if (event == LV_EVENT_KEY)
    {
        printf("LV_EVENT_KEY event=%d\r\n", event);

        uint32_t* PAGE_INDEX = lv_event_get_param(e);
        printf("key code %d\r\n", *PAGE_INDEX);

      

        //lv_obj_t *obj = lv_event_get_target(e);
        //int code = 0x10;
        //lv_event_send(obj, LV_EVENT_MENU_CHANGE, (void*)&code);
        
        
    }
    else if (event == LV_EVENT_MENU_CHANGE)
    {
        //printf("LV_EVENT_MENU_CHANGE event=%d\r\n", event);
        uint32_t* PAGE_INDEX = lv_event_get_param(e);
        printf("LV_EVENT_MENU_CHANGE key code %d\r\n", *PAGE_INDEX);

        page_config.current_page = *PAGE_INDEX;

        Menu_Switch_Callback(page_config.current_page, page_config.last_page);

    }
    else if (event == LV_EVENT_WINDOW)
    {

        lv_obj_t* obj = lv_event_get_current_target(e);
        //uint32_t id = lv_obj_get_index(obj); //获取界面索引选中跳入界面
        //printf("LV_EVENT_WINDOW event=%d,child %d\r\n", event, id);

        uint32_t* window_type = lv_event_get_param(e);
        printf("LV_EVENT_WINDOW window_type code %d\r\n", *window_type);
        page_config.window_type = *window_type;

        /*lv_obj_t* parent_obj = NULL;
        if ((page_config.window_type == DISPLAY_WINDOW_RESET_DEFAULT_YES) ||
            (page_config.window_type == DISPLAY_WINDOW_RESET_DEFAULT_NO))
            parent_obj = lv_obj_get_parent(obj);
        else {
            parent_obj = obj;
        }*/
        

        Window_Display_Callback(page_config.window_type, obj);
    }


    
}


static void meter2_timer_cb(lv_timer_t* timer)
{

    page_config.local_fast->fast_value = page_config.local_fast->fast_grade_value * 10;

    lv_img_set_src(fast_img, fast_value_image[page_config.local_fast->fast_grade_value]);//
    page_config.local_fast->fast_grade_value++;

     lv_label_set_text_fmt(fast_value_label, "%d %s", page_config.local_fast->fast_value,"%");
   
    //printf(" page_config.local_fast->fast_grade_value = %d\r\n", page_config.local_fast->fast_grade_value);
    if (page_config.local_fast->fast_grade_value >= 10)
    {
        page_config.local_fast->fast_grade_value = 0;
    }



    //测试方位数据
    page_config.compass.compass_data.int_value += 10;

    if (page_config.compass.compass_data.dir_value == 0)
    {
        lv_label_set_text_fmt(compass, "%d°", page_config.compass.compass_data.int_value);
    }
    else  if (page_config.compass.compass_data.dir_value == 1)
    {
        lv_label_set_text_fmt(compass, "N%d°", page_config.compass.compass_data.int_value);
    }
    else if (page_config.compass.compass_data.dir_value == 2)
    {
        lv_label_set_text_fmt(compass, "S%d°", page_config.compass.compass_data.int_value);
    }
    else
        if (page_config.compass.compass_data.dir_value == 3)
        {
            lv_label_set_text_fmt(compass, "W%d°", page_config.compass.compass_data.int_value);
        }
        else
            if (page_config.compass.compass_data.dir_value == 4)
            {
                lv_label_set_text_fmt(compass, "E%d°", page_config.compass.compass_data.int_value);
            }




}

/*
    进入主页界面 释放内存
*/
void user_main_page_entry(lv_obj_t* obj)
{

    page_config.current_page = MAIN_MENU;
	
	lv_obj_set_style_pad_all(obj, 0, 0);
    lv_obj_set_style_border_width(obj, 0, 0);
    lv_obj_set_style_bg_color(obj, lv_color_black(), 0);
    lv_obj_set_size(obj, LV_PCT(100), LV_PCT(100));
    lv_obj_set_scrollbar_mode(obj, LV_SCROLLBAR_MODE_OFF);

    
    //page_config.dev_batt[i].batt_grade_value_is_display = true;
    page_config.dev_batt[0].batt_grade_value = 1;
    page_config.dev_batt[0].batt_value_is_display = true;
    page_config.dev_batt[0].batt_grade_value_is_display = true;
   

    page_config.dev_batt[1].batt_value_is_display = true;
    page_config.dev_batt[1].batt_grade_value_is_display = true;
    page_config.dev_batt[1].batt_grade_value = 2;

    page_config.dev_batt[2].batt_value_is_display = true;
    page_config.dev_batt[2].batt_grade_value_is_display = true;
    page_config.dev_batt[2].batt_grade_value = 4;

    page_config.dev_batt[3].batt_value_is_display = true;
    page_config.dev_batt[3].batt_grade_value_is_display = true;
    page_config.dev_batt[3].batt_grade_value = 4;


    page_config.dev_sign[0].sign_grade_value = 1;
    page_config.dev_sign[0].is_display = true;
    page_config.dev_sign[1].sign_grade_value = 3;
    page_config.dev_sign[1].is_display = true;
    page_config.dev_sign[2].sign_grade_value = 2;
    page_config.dev_sign[3].sign_grade_value = 4;

    page_config.dev_sign[2].is_display = true;
    page_config.dev_sign[3].is_display = true;

    page_config.local_batt->batt_grade_value = 1;

    //深度数据配置
    page_config.deep.deep_all = 0;
    page_config.deep.deep_data.int_value = 200;
    page_config.deep.deep_data.point_value = 8;
    page_config.deep.deep_data.int_type = 0;


    page_config.compass.compass_data.int_value = 0;
    page_config.compass.compass_data.dir_value = 3;

    
    for (int i = 0; i < 4; i++) //
    {
         bat_img[i] = lv_img_create(obj);  //显示电池

        bat_value_label[i] = lv_label_create(obj);  //显示电池数值

        //lv_label_set_recolor(bat_value_label[i], true);                        /*Enable re-coloring by commands in the text*/
        //lv_label_set_text_fmt(bat_value_label[i], "#ffffff %s", "\#100");
        //lv_obj_set_style_text_font(bat_value_label[i], &font18, 0);
        //lv_obj_set_style_text_color(bat_value_label[i], lv_color_make(0xff, 0xff, 0xff), 0);
        //char buffer[10] = {0};
        //sprintf(buffer,"\u0023 %d", page_config.dev_batt[i].batt_value);
        //lv_label_set_text(bat_value_label[i],"#");
        lv_label_set_text_fmt(bat_value_label[i], "#%d", page_config.dev_batt[i].batt_value);

        lv_obj_set_pos(bat_value_label[i], bat_pos[i].x, bat_pos[i].y - 20);

        if (page_config.dev_batt[i].batt_value_is_display == true)
        {
            lv_obj_clear_flag(bat_value_label[i], LV_OBJ_FLAG_HIDDEN);
        }
        else
        {
            lv_obj_add_flag(bat_value_label[i], LV_OBJ_FLAG_HIDDEN);
        }

        lv_img_set_src(bat_img[i], batt_value_image[page_config.dev_batt[i].batt_grade_value]);//设置图片源
        lv_obj_set_pos(bat_img[i], bat_pos[i].x, bat_pos[i].y);

        if (page_config.dev_batt[i].batt_grade_value_is_display == true)
        {
            lv_obj_clear_flag(bat_img[i], LV_OBJ_FLAG_HIDDEN);
        }
        else
        {
            lv_obj_add_flag(bat_img[i], LV_OBJ_FLAG_HIDDEN);
        }

        
        sign_img[i] = lv_img_create(obj); //显示信号
        lv_img_set_src(sign_img[i], sign_value_image[page_config.dev_sign[i].sign_grade_value]);//设置图片源
        lv_obj_set_pos(sign_img[i], bat_pos[i].x+30, bat_pos[i].y);


        if (page_config.dev_sign[i].is_display == true)
        {
            lv_obj_clear_flag(sign_img[i], LV_OBJ_FLAG_HIDDEN);
        }
        else
        {
            lv_obj_add_flag(sign_img[i], LV_OBJ_FLAG_HIDDEN);
        }

    }


    fast_img = lv_img_create(obj);
    lv_img_set_src(fast_img, fast_value_image[page_config.local_fast->fast_grade_value]);//设置图片源
    lv_obj_set_pos(fast_img, 110, 51);


    fast_value_label  = lv_label_create(obj);  //显示电池数值

    lv_label_set_recolor(fast_value_label, true);                        /*Enable re-coloring by commands in the text*/
    //lv_label_set_text_fmt(fast_value_label, "#ffffff %s", "\#100");

    lv_obj_set_style_text_font(fast_value_label, &font18, 0);
    lv_obj_set_style_text_color(fast_value_label, lv_color_make(0xff, 0xff, 0xff), 0);

 
    lv_label_set_text_fmt(fast_value_label, "%d %s", page_config.local_fast->fast_value,"%");

    lv_obj_set_pos(fast_value_label, 134,20);




    local_bat_img = lv_img_create(obj);  //显示本地电池

    lv_img_set_src(local_bat_img, batt_value_image[page_config.local_batt->batt_grade_value]);//设置图片源
    lv_obj_set_pos(local_bat_img,66,26);





    local_time_label = lv_label_create(obj);  //显示电池数值

    lv_label_set_recolor(local_time_label, true);                        /*Enable re-coloring by commands in the text*/
    //lv_label_set_text_fmt(fast_value_label, "#ffffff %s", "\#100");

    lv_obj_set_style_text_font(local_time_label, &font18, 0);
    lv_obj_set_style_text_color(local_time_label, lv_color_make(0xff, 0xff, 0xff), 0);


    lv_label_set_text_fmt(local_time_label, "%02d:%02d", page_config.local_fast->fast_value, page_config.local_fast->fast_value);

    lv_obj_set_pos(local_time_label, 40, 44);





    //深度数值显示
    deep_label = lv_label_create(obj);

    lv_label_set_recolor(deep_label, true);                        /*Enable re-coloring by commands in the text*/
    //lv_label_set_text_fmt(fast_value_label, "#ffffff %s", "\#100");

    lv_obj_set_style_text_font(deep_label, &font18, 0);
    lv_obj_set_style_text_color(deep_label, lv_color_make(0xff, 0xff, 0xff), 0);


    if (page_config.deep.deep_data.int_type == 0)
    {
        lv_label_set_text_fmt(deep_label, "-%02d.%01dm", page_config.deep.deep_data.int_value, page_config.deep.deep_data.point_value);
    }
    else  if (page_config.deep.deep_data.int_type == 1)
    {
        lv_label_set_text_fmt(deep_label, "%02d.%01dm", page_config.deep.deep_data.int_value, page_config.deep.deep_data.point_value);
    }
   

    lv_obj_set_pos(deep_label, 54, 174);
  


      //温度数值显示
    temp_label = lv_label_create(obj);

    lv_label_set_recolor(temp_label, true);                        /*Enable re-coloring by commands in the text*/
    //lv_label_set_text_fmt(fast_value_label, "#ffffff %s", "\#100");

    lv_obj_set_style_text_font(temp_label, &font18, 0);
    lv_obj_set_style_text_color(temp_label, lv_color_make(0xff, 0xff, 0xff), 0);


    if (page_config.deep.deep_data.int_type == 0)
    {
        lv_label_set_text_fmt(temp_label, "-%02d.%01d℃", page_config.deep.deep_data.int_value, page_config.deep.deep_data.point_value);
    }
    else  if (page_config.deep.deep_data.int_type == 1)
    {
        lv_label_set_text_fmt(temp_label, "%02d.%01d℉", page_config.deep.deep_data.int_value, page_config.deep.deep_data.point_value);
    }
    lv_obj_set_pos(temp_label, 128, 174);






    //方位显示
    compass = lv_label_create(obj);

    lv_label_set_recolor(compass, true);                        /*Enable re-coloring by commands in the text*/
    //lv_label_set_text_fmt(fast_value_label, "#ffffff %s", "\#100");

    lv_obj_set_style_text_font(compass, &font18, 0);
    lv_obj_set_style_text_color(compass, lv_color_make(0xff, 0xff, 0xff), 0);


    if (page_config.compass.compass_data.dir_value == 0)
    {
        lv_label_set_text_fmt(compass, "%d°", page_config.compass.compass_data.int_value);
    }
    else  if (page_config.compass.compass_data.dir_value == 1)
    {
        lv_label_set_text_fmt(compass, "N%d°", page_config.compass.compass_data.int_value);
    }else if(page_config.compass.compass_data.dir_value == 2)
    {
        lv_label_set_text_fmt(compass, "S%d°", page_config.compass.compass_data.int_value);
    }else
        if (page_config.compass.compass_data.dir_value == 3)
    {
        lv_label_set_text_fmt(compass, "W%d°", page_config.compass.compass_data.int_value);
    }else
    if (page_config.compass.compass_data.dir_value == 4)
    {
        lv_label_set_text_fmt(compass, "E%d°", page_config.compass.compass_data.int_value);
    }

    lv_obj_align(compass, LV_ALIGN_TOP_MID, 0, 124);

    //lv_obj_set_pos(compass, 94, 124);




		



		
		
		
   
		
    lv_obj_t* line1 = NULL;
    line1 = lv_line_create(obj);
    lv_line_set_points(line1, line_points1, 2);     /*Set the points*/
		
    lv_obj_t* line2 = NULL;
    line2 = lv_line_create(obj);
    lv_line_set_points(line2, line_points2, 2);     /*Set the points*/
	

	lv_obj_t* deep = lv_img_create(obj);
    lv_img_set_src(deep, &depth);//设置深度图片源
	lv_obj_set_pos(deep,84, 210);




	lv_obj_t* temp = lv_img_create(obj);
    lv_img_set_src(temp, &temperature);//设置温度图片源
	lv_obj_set_pos(temp,140, 209);



    lv_obj_t* bg = lv_img_create(obj);
    lv_img_set_src(bg, &bg1);//设置温度图片源
    lv_obj_set_align(bg, LV_ALIGN_CENTER);
    

     lv_obj_add_event_cb(obj, main_event_handler, LV_EVENT_KEY, obj);
     lv_obj_add_event_cb(obj, main_event_handler, LV_EVENT_MENU_CHANGE, obj);
     lv_obj_add_event_cb(obj, main_event_handler, LV_EVENT_WINDOW, obj);
     lv_obj_add_event_cb(obj, main_event_handler, LV_EVENT_FOCUSED, obj);
     lv_obj_add_event_cb(obj, main_event_handler, LV_EVENT_DEFOCUSED, obj);
     lv_obj_add_event_cb(obj, main_event_handler, LV_EVENT_CLICKED, obj);
     
     

     timer = lv_timer_create(meter2_timer_cb, 200, NULL);
     lv_group_focus_obj(obj);
     lv_group_add_obj(g_group, obj);

}




/*
    退出主页界面 释放内存
*/
void user_main_page_exit(lv_obj_t* obj)
{
    lv_timer_del(timer);
    if (lv_group_get_obj_count(g_group) > 0)
    {
        //printf("group %d\r\n",lv_group_get_obj_count(g_group));
        lv_group_remove_all_objs(g_group);
    }
        //printf("group = 0,lv_obj_get_child_cnt = %d\r\n", lv_obj_get_child_cnt(obj));
        lv_obj_clean(obj);

        page_config.last_page =  page_config.current_page;

}
